/**
 * 
 */
package com.yhty.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yhty.dao.HouseDao;
import com.yhty.dao.OweDao;
import com.yhty.dao.RentDao;
import com.yhty.dao.WeDefineDao;
import com.yhty.model.House;
import com.yhty.model.Owe;
import com.yhty.model.Pay;
import com.yhty.model.Rent;
import com.yhty.model.WeDefine;
import com.yhty.service.RentManager;
import com.yhty.utils.DateUtil;

/**
 * 房屋出租业务逻辑实现类
 * @author sccdqq
 */
@Service("rentManager")
@Transactional
public class RentManagerImpl implements RentManager {
	private RentDao rentDao;
	private OweDao oweDao;
	private WeDefineDao weDefineDao;
	private HouseDao houseDao;
	
	@Resource(name="rentDao")
	public void setRentDao(RentDao rentDao) {
		this.rentDao = rentDao;
	}
	
	/* 建立出租关系 changed by roy
	 * @see com.yhty.service.RentManager#rent(com.yhty.model.Rent)
	 */
	public void rent(Rent r) {
		//将房屋设置为已出租状态
		House h = houseDao.getByFileCode(r.getFile_code());
		if(h.getStatus().equals("1"))
			throw new RuntimeException("此房屋已经出租");
		h.setStatus("1");
		houseDao.update(h);
		if(DateUtil.isSameMonth(r.getR_date())){
			Owe owe = new Owe();
			owe.setRent_id(this.rentDao.add(r));
			WeDefine wd = weDefineDao.get();
			Pay pay = new Pay();
			pay.setRent(r.getRental());
			pay.setManager(r.getManagmentExpenses());
			pay.setWater(h.getUse_area()*wd.getWater_price());
			owe.setOwe(pay);
			owe.setCreate_date(DateUtil.getToday());
			oweDao.add(owe);
		}
	}
	
	/* 退房并将房屋信息置为空闲状态
	 * @see com.yhty.service.RentManager#remove(com.yhty.model.Rent)
	 */
	public void remove(Long id) {
		this.rentDao.remove(id);
		Rent r = rentDao.getById(id);
		House h = houseDao.getByFileCode(r.getFile_code());
		h.setStatus("0");
		houseDao.update(h);
	}
	
	/* (non-Javadoc)
	 * @see com.yhty.service.RentManager#getAll(com.yhty.model.Rent)
	 */
	public List<Rent> getAll() {
		return this.rentDao.getAll();
	}

	@Resource(name="oweDao")
	public void setOweDao(OweDao oweDao) {
		this.oweDao = oweDao;
	}

	@Resource(name="weDefineDao")
	public void setWeDefineDao(WeDefineDao weDefineDao) {
		this.weDefineDao = weDefineDao;
	}

	@Resource(name="houseDao")
	public void setHouseDao(HouseDao houseDao) {
		this.houseDao = houseDao;
	}
}
